package com.example.ex2.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ex2.domain.Item;
import com.example.ex2.persistence.ItemMapper;
import com.example.ex2.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Override
    public boolean addItemByItem(Item item){
        return (itemMapper.insert(item) > 0);
    }

    @Override
    public boolean deleteItemById(int id){
        return (itemMapper.deleteById(id) > 0);
    }

    @Override
    public List<Item> selectAllItems(){
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        return itemMapper.selectList(queryWrapper);
    }

    @Override
    public boolean updateItemByItem(Item item){
        return (itemMapper.updateById(item) > 0);
    }

    @Override
    public String selectNameByItemId(int id){
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_id",id);
        return itemMapper.selectOne(queryWrapper).getName();
    }
}
